﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SDT.Lib.Paging;
using System.Linq.Dynamic;


namespace SDT.Model
{
    public class CompanyManager
    {
        public static IPagedList<Company> GetList(int? typeid, int? cityid, int? distrid, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new SDTDataContext();
            var l = dataContext.Companies
                .Where(a => typeid > 0 ? a.CompanyTypeMaps.Where(e=>e.CompanyTypeID==typeid).ToList().Count>0 : true)
                .Where(a => cityid > 0 ? a.CityID == cityid : true)
                .Where(a => distrid > 0 ? a.DistrictID == distrid : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.ActiveYN == (pubYN == 1 ? true : false) : true)
                .Where(a => keyword == string.Empty ? true : (a.Name.Contains(keyword.ToLower())))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }
        public static IList<Company> GetList(int number, int typeid, int cityid, int distrid, int hotYN, int pubYN, string order, string orderDir)
        {
            var dataContext = new SDTDataContext();
            var l = dataContext.Companies
                .Where(a => typeid > 0 ? a.CompanyTypeMaps.Where(e => e.CompanyTypeID == typeid).ToList().Count > 0 : true)
                .Where(a => hotYN > 0 ? a.HotYN == true : true)
                .Where(a => pubYN > 0 ? a.ActiveYN == true : true)
                .Where(a => cityid > 0 ? a.CityID == cityid : true)
                .Where(a => distrid > 0 ? a.DistrictID == distrid : true)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var rt = l.Take(number).ToList();
            return rt;
        }
    }
}
